<template>
  <el-form :model="loginForm" :rules="fieldRules" ref="loginForm" label-position="left" label-width="0px"
           class="login-container">
    <span class="tool-bar"></span>
    <h2 class="title" style="padding-left: 22px;">系统登录</h2>
    <el-form-item prop="account">
      <el-input type="text"
                v-model="loginForm.account"
                auto-complete="off"
                placeholder="账号">
      </el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input type="password"
                v-model="loginForm.password"
                show-password
                auto-complete="off"
                placeholder="密码">
      </el-input>
    </el-form-item>
    <el-form-item>
      <el-col :span="12">
        <el-form-item prop="captcha">
          <el-input type="test"
                    v-model="loginForm.captcha"
                    auto-complete="off"
                    placeholder="验证码，单击图片刷新"
                    style="width: 100%">
          </el-input>
        </el-form-item>
      </el-col>
      <el-col class="line" :span="1">&nbsp;</el-col>
      <el-col :span="11">
        <el-form-item>
          <img :src="loginForm.src" style="width: 100%;" class="pointer" @click="refreshCaptcha"/>
        </el-form-item>
      </el-col>
    </el-form-item>
    <el-checkbox class="login_remember" v-model="checked" label-position="left">记住密码</el-checkbox>
    <el-form-item style="width: 100%">
      <el-button type="primary" style="width: 48%;" @click.native.prevent="reset">重 置</el-button>
      <el-button type="primary" style="width: 48%;" @click.native.prevent="login" :loading="loading">登 录</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import Cookies from 'js-cookie'

  export default {
    name: "Login",
    data() {
      return {
        loading: false,
        loginForm: {
          account: 'admin',
          password: '',
          captcha: '',
          src: ''
        },
        fieldRules: {
          account: [
            {require: true, message: '请输入账号', trigger: 'blur'}
          ],
          password: [
            {require: true, message: '请输入密码', trigger: 'blur'}
          ],
          captcha: [
            {require: true, message: '请输入验证码', trigger: 'blur'}
          ]
        },
        checked: true
      }
    },
    methods: {
      //登录
      login() {
        this.loading = true;
        let userInfo = {
          account: this.loginForm.account,
          password: this.loginForm.password,
          captcha: this.loginForm.captcha
        };
        this.$api.login.login(userInfo).then((res) => {
          if (res.code === 200) {
            //设置token到Cookie
            localStorage.setItem('token', res.data.token);
            localStorage.setItem('authorizedShops', res.data.authorizedShops);
            //TODO 获取用户信息保存下来 保存用户到本地会话
            localStorage.setItem('account', userInfo.account);
            localStorage.setItem('password', userInfo.password);
            //登录成功后，加载导航菜单
            this.$store.commit('menuRouteLoaded', false);
            //登录成功， 跳转到首页
            this.$router.push('/');
          } else {
            this.$message({message: res.message, type: 'error'});
          }
        }).catch((res) => {
          this.$message({message: res.message, type: 'error'});
        });
        this.loading = false;
      },
      //验证码
      refreshCaptcha: function () {
        this.loginForm.src = this.global.baseUrl + '/captcha.jpg?t=' + new Date().getTime();
      },
      //重置
      reset() {
        this.$refs.loginForm.resetFields();
      }
    },
    //初始化
    mounted() {
      this.refreshCaptcha();
    }
  }
</script>

<style scoped>
  .login-container {
    border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    background-clip: padding-box;
    margin: 100px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }

  .login-container .title {
    margin: 0 auto 30px auto;
    text-align: center;
    color: #505458;
  }

  .login_remember {
    margin: 0 0 35px 0;
    text-align: left;
  }
</style>
